﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.ComponentModel;

namespace EasyProp.Tests
{
    [TestFixture]
    public class BeforePropertySetFilterFixture
    {
        [Test]
        public void executes_command()
        {
            EasyPropBuilder easyPropBuilder = new EasyPropBuilder();
            ClassWithOneAttribute theObject = easyPropBuilder.Build<ClassWithOneAttribute>();

            CountInvocationsFilter.Count = 0;
            theObject.MyProperty = "tjohoo";
            Assert.AreEqual(1, CountInvocationsFilter.Count);
        }

        [Test]
        public void calls_setter_body_before_calling_filter()
        {
            EasyPropBuilder easyPropBuilder = new EasyPropBuilder();
            ClassWithSetterBody theObject = easyPropBuilder.Build<ClassWithSetterBody>();

            theObject._status = "initial value";

            theObject.PropertyChanged += (o, e) => Assert.AreEqual("initial value", theObject._status);

            theObject.Status = "new value";
        }

        [BeforePropertySetFilter(typeof(CountInvocationsFilter))]
        public class ClassWithOneAttribute
        {
            public virtual string MyProperty { get; set; }
        }

        [BeforePropertySetFilter(typeof(NotifyPropertyChanged))]
        public class ClassWithSetterBody : INotifyPropertyChanged
        {
            public virtual string Status
            {
                get { return _status; }
                set
                {
                    _status = value;
                }
            }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            void GetRidOfCompilerWarnings()
            {
                this.PropertyChanged(null, null);
            }

            #endregion

            public string _status;
        }
    }
}
